using System;
using UnityEngine;

namespace VoxelGame.Character
{
	[Serializable]
	public class CharacterToolsController
	{
		[Serializable]
		public class ToolData
		{
			public enum Tools
			{
				None,
				Pickaxe,
				TNT,
				Drill,
				Portal
			}

			[SerializeField]
			private Tools _toolType;

			[SerializeField]
			private CharacterTool _characterTool;

			public CharacterTool ToolInstance
			{
				get;
				set;
			}

			public Tools ToolType => _toolType;

			public CharacterTool CharacterTool => _characterTool;
		}

		[SerializeField]
		private ToolData[] _tools;

		[SerializeField]
		private Transform _anchor;

		[SerializeField]
		private ToolData.Tools _defautType;

		private CharacterTool _currentTool;

		private Action<ToolData.Tools> _onToolSelected;

		public CharacterTool CurrentTool => _currentTool;

		public void SetupTools(Action<ToolData.Tools> onToolSelected, CharacterSoundController soundController)
		{
			_onToolSelected = onToolSelected;
			ToolData[] tools = _tools;
			foreach (ToolData toolData in tools)
			{
				toolData.ToolInstance = UnityEngine.Object.Instantiate(toolData.CharacterTool);
				toolData.ToolInstance.transform.SetParentAndCopyTransform(_anchor);
				toolData.ToolInstance.gameObject.SetActive(value: false);
				toolData.ToolInstance.Setup(soundController);
			}
			EnableTool(_defautType);
		}

		public void EnableTool(ToolData.Tools type)
		{
			if (_currentTool != null)
			{
				_currentTool.gameObject.SetActive(value: false);
			}
			ToolData[] tools = _tools;
			foreach (ToolData toolData in tools)
			{
				if (toolData.ToolType == type)
				{
					_currentTool = toolData.ToolInstance;
					_currentTool.gameObject.SetActive(value: true);
					break;
				}
			}
			_onToolSelected.Fire(type);
		}
	}
}
